/* =========================================================================
 *
 *   The traffic processor.
 *
 *   Development by Sedelnikov Alexander (Bigking).
 *
 * =========================================================================
 */

#include "ipacc3_parser.h"
#include <string.h>

//-----------------------------------------------------------------------//
time_stamp cipacc3_parser::get_time( const char *name )
{
    char       line[ 256 ], text[ 256 ];
    time_stamp ts;

    input->close();
    input->open( name );
    input->gets( line, 256 );

    strncpy( text, line, 4 ); text[ 4 ] = 0;
    ts.year = atoi( text );
    strncpy( text, line + 4, 2 ); text[ 2 ] = 0;
    ts.month = atoi( text );
    strncpy( text, line + 6, 2 ); text[ 2 ] = 0;
    ts.day = atoi( text );

    strncpy( text, line + 8, 2 ); text[ 2 ]  = 0;
    ts.hour   = atoi( text );
    strncpy( text, line + 10, 2 ); text[ 2 ] = 0;
    ts.minute = atoi( text );


    return ts;
}


//-----------------------------------------------------------------------//
void cipacc3_parser::start( const char *name )
{
    input->open( name );

    char  text_line[ 256 ] = { 0, 0, 0, 0 };

    while( !input->eof( ) && text_line[ 3 ] != 'S' )
    {
        input->gets( text_line, 255 );
    }

    if( input->eof( ) )
    {
        THROW( e_wrong, "Wrong [IPACC3] a file format!" );
    }
}


//-----------------------------------------------------------------------//
int  cipacc3_parser::get_next( parser_item &item )
{
    uint   ip[ 4 ] = { 0, 0, 0, 0 },
           index_word[ 8 ],
           i, j ,k;


    char   line[ 255 ] = { 0 };


    if( input->eof() )
        return 1;

    input->gets( line, 255 );

    if( line[ 0 ] == 0x0A || line[ 0 ] == 0x0D )
    {
        return 1;
    }

    k = 0;
    j = strlen( line );

    for( i = 0; i < j; )
    {
        if( line[ i ] != ' ' )
        {
            index_word[ k ] = i;

            for( ; i < j; i++ )
            {
                if( line[ i ] == ' ' || line[ i ] == 0x0A || line[ i ] == 0x0D )
                {
                    line[ i ] = 0;
                    i++;
                    break;
                }
            }

            k++;
        }
        else
            i++;
    }

    if( k < 8 )
    {
        THROW( e_demage, "[IPACC3] file is demage!" );
    }


    sscanf( line + index_word[ 2 ], "%ld", &item.packets );
    sscanf( line + index_word[ 3 ], "%ld", &item.bytes );
    sscanf( line + index_word[ 4 ], "%ld", &item.port_src );
    sscanf( line + index_word[ 5 ], "%ld", &item.port_dst );
    sscanf( line + index_word[ 6 ], "%ld", &item.protocol );


    sscanf( line + index_word[ 0 ], "%ld.%ld.%ld.%ld",
        &ip[ 0 ], &ip[ 1 ], &ip[ 2 ], &ip[ 3 ] );

    item.ip_src =  ( ( ip[ 3 ] & 255 ) << 24 ) |
                   ( ( ip[ 2 ] & 255 ) << 16 ) |
                   ( ( ip[ 1 ] & 255 ) << 8 ) |
                   ( ( ip[ 0 ] & 255 ) );


    sscanf( line + index_word[ 1 ], "%ld.%ld.%ld.%ld",
        &ip[ 0 ], &ip[ 1 ], &ip[ 2 ], &ip[ 3 ] );

    item.ip_dst =  ( ( ip[ 3 ] & 255 ) << 24 ) |
                   ( ( ip[ 2 ] & 255 ) << 16 ) |
                   ( ( ip[ 1 ] & 255 ) << 8 ) |
                   ( ( ip[ 0 ] & 255 ) );


    return 0;
}
